import { Toast } from 'antd-mobile';
import baidu from '../utils/baidu';

export default {
  namespace: 'home',
  state: {
    selected: 'tab1',
    homeSchoolTab: '1',
  },
  reducers: {
    save(state, { payload: { selected } }) {
      return { ...state, selected };
    },
    save2(state, { payload }) {
      return { ...state, ...payload };
    },
    tip(state, { payload: { type, message } }) {
      Toast[type](message);
      return state;
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        // baidu(pathname, query, dispatch);
        dispatch({ type: 'auth/baidu', payload: { pathname, query } });
        if (pathname === '/') {
          dispatch({ type: 'save', payload: query });
          dispatch({ type: 'save2', payload: query });
          dispatch({ type: 'auth/fetch_saleman_list' });
        }
      });
    },
  },
};
